package com.example.xuanmei.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.xuanmei.data.api.ApiClient
import com.example.xuanmei.data.model.User
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

class UserViewModel : ViewModel() {
    private val _currentUser = MutableStateFlow<LoginResponse?>(null)
    val currentUser: StateFlow<LoginResponse?> = _currentUser.asStateFlow()
    
    private val _userInfo = MutableStateFlow<User?>(null)
    val userInfo: StateFlow<User?> = _userInfo.asStateFlow()
    
    private val _loading = MutableStateFlow(false)
    val loading: StateFlow<Boolean> = _loading.asStateFlow()
    
    private val _error = MutableStateFlow<String?>(null)
    val error: StateFlow<String?> = _error.asStateFlow()
    
    data class LoginResponse(
        val userId: Long,
        val username: String,
        val token: String
    )
    
    fun login(username: String, password: String) {
        viewModelScope.launch(Dispatchers.IO) {
            try {
                _loading.value = true
                _error.value = null
                
                val response = ApiClient.apiService.login(
                    mapOf(
                        "username" to username,
                        "password" to password
                    )
                )
                
                response.execute().body()?.let { apiResponse ->
                    if (apiResponse.code == 200) {
                        _currentUser.value = LoginResponse(
                            apiResponse.data?.userId ?: 0,
                            apiResponse.data?.username ?: "",
                            apiResponse.data?.token ?: ""
                        )
                    } else {
                        _error.value = apiResponse.message
                    }
                } ?: run {
                    _error.value = "网络请求失败"
                }
            } catch (e: Exception) {
                _error.value = e.message ?: "登录失败"
            } finally {
                _loading.value = false
            }
        }
    }
    
    fun register(username: String, password: String, nickname: String) {
        viewModelScope.launch(Dispatchers.IO) {
            try {
                _loading.value = true
                _error.value = null
                
                val response = ApiClient.apiService.register(
                    mapOf(
                        "username" to username,
                        "password" to password,
                        "nickname" to nickname
                    )
                )
                
                response.execute().body()?.let { apiResponse ->
                    if (apiResponse.code == 200) {
                        _currentUser.value = LoginResponse(
                            apiResponse.data?.userId ?: 0,
                            apiResponse.data?.username ?: "",
                            apiResponse.data?.token ?: ""
                        )
                    } else {
                        _error.value = apiResponse.message
                    }
                } ?: run {
                    _error.value = "网络请求失败"
                }
            } catch (e: Exception) {
                _error.value = e.message ?: "注册失败"
            } finally {
                _loading.value = false
            }
        }
    }
}
